<?php
class RP_Model_User_JobseekerMapper{
	protected $_dbTable;
    
    public function setDbTable ($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (! $dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }
    
    public function getDbTable ()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('RP_Model_DbTable_AuthUser');
        }
        return $this->_dbTable;
    }
	public function register(RP_Model_User_Jobseeker $jskr){
		$logger = Zend_Registry::get("logger");
		try{
			$this->getDbTable()
                ->getAdapter()
                ->beginTransaction();
                
            $authData = array(
            		'user_type_id'=>$jskr->getUser_type_id(),
					'username'=>$jskr->getUsername(),
					'password'=>$jskr->getPassword(),
					'email'=>$jskr->getEmail(),
					);
            $logger->debug($authData);
            $pKey = $this->getDbTable()->insert($authData);
            $jskr->setUser_id($pKey);
            
            $userRole = new RP_Model_DbTable_UserRole();
            $userRole->insert(
            array('user_id' => $jskr->getUser_id(), 'role_id' => 'jobseeker'));
            
            $seekerData = array(
            				'user_id'=>$jskr->getUser_id(),
            				'name'=>$jskr->getName(),
            				'mobile_no'=>$jskr->getMobile_no(),
            				'alternate_no'=>$jskr->getAlternate_no()
            			);
            if(!$jskr->getUg_check()){
            	$seekerData['highest_qualification'] = $jskr->getCourse_type(); 
            }
            else{
            	$seekerData['highest_qualification'] = "UG";
            }
            
            $seekerPersonal = new RP_Model_DbTable_SeekerPersonal();
            $seekerPersonal->insert($seekerData);
            
            $proData = array(
            				'user_id' => $jskr->getUser_id(),
            				'curr_location'=>$jskr->getCurr_location(),
            				'skills'=>$jskr->getSkills(),
 							'resume_headline'=>$jskr->getResume_headline()	
            			);
			if($jskr->getIndustry_id()!=0){
            	$proData['industry_id'] = $jskr->getIndustry_id(); 
            }
			if($jskr->getFa_id()!=0){
            	$proData['fa_id'] = $jskr->getFa_id(); 
            }
            if($jskr->getExp_years()!=''){
            	$proData['exp_years'] = $jskr->getExp_years(); 
            }
			if($jskr->getExp_months()!=''){
            	$proData['exp_months'] = $jskr->getExp_months(); 
            }
            			     
            $seekerPro = new RP_Model_DbTable_SeekerProfessional();
            $seekerPro->insert($proData);
            
            
			$logger->debug($jskr->getUg_check());
            
            if($jskr->getUg_check()== 'true'){
            	$this->getDbTable()
                ->getAdapter()
                ->commit();
            }
            else{
            	$eduData = array(
            					'user_id'=>$jskr->getUser_id(),
            					'edu_spec_id'=>$jskr->getEdu_spec_id(),
            					'course_id'=>$jskr->getCourse_id(),
            					'edu_org_id'=>$jskr->getEdu_org_id(),
            					'edu_style'=> $jskr->getEdu_style(),
            					'year'=>$jskr->getYear_of_passing()
            				);
            	$education = new RP_Model_DbTable_Education();
            	$education->insert($eduData);
            		$this->getDbTable()
                ->getAdapter()
                ->commit();
            
            }
		}
		catch(Exception $e){
			$this->getDbTable()
                ->getAdapter()
                ->rollBack();             
            throw $e;
		}
		return $jskr;
	}
    
}//End class
?>